﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using DingTalkAgent.DBUtility;

namespace DingTalkAgent.SqlData
{
    public partial class SysAction
    {
        #region	 T_SysAction

        ///<summary>
        /// Exist
        ///</summary>
        public bool Exists(int Act_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_SysAction");
            strSql.Append(" where ");
            strSql.Append(" Act_ID = @Act_ID  ");
            SqlParameter[] parameters = {
                    new SqlParameter("@Act_ID", SqlDbType.Int,4)
};
            parameters[0].Value = Act_ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条T_SysAction数据
        /// </summary>
        public int Add(DingTalkAgent.Model.SysAction model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_SysAction(");
            strSql.Append("Page_ID,Act_Name,Controller,Action");
            strSql.Append(") values (");
            strSql.Append("@Page_ID,@Act_Name,@Controller,@Action");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                        new SqlParameter("@Page_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@Act_Name", SqlDbType.NVarChar,32) ,
                        new SqlParameter("@Controller", SqlDbType.VarChar,64) ,
                        new SqlParameter("@Action", SqlDbType.VarChar,64)

            };

            parameters[0].Value = model.Page_ID;
            parameters[1].Value = model.Act_Name;
            parameters[2].Value = model.Controller;
            parameters[3].Value = model.Action;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }

        }


        /// <summary>
        /// 更新一条T_SysAction数据
        /// </summary>
        public bool Update(DingTalkAgent.Model.SysAction model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_SysAction set ");

            strSql.Append(" Page_ID = @Page_ID , ");
            strSql.Append(" Act_Name = @Act_Name , ");
            strSql.Append(" Controller = @Controller , ");
            strSql.Append(" Action = @Action  ");
            strSql.Append(" where Act_ID=@Act_ID ");

            SqlParameter[] parameters = {
                        new SqlParameter("@Act_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@Page_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@Act_Name", SqlDbType.NVarChar,32) ,
                        new SqlParameter("@Controller", SqlDbType.VarChar,64) ,
                        new SqlParameter("@Action", SqlDbType.VarChar,64)

            };

            parameters[0].Value = model.Act_ID;
            parameters[1].Value = model.Page_ID;
            parameters[2].Value = model.Act_Name;
            parameters[3].Value = model.Controller;
            parameters[4].Value = model.Action;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条T_SysAction数据
        /// </summary>
        public bool Delete(int Act_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_SysAction ");
            strSql.Append(" where Act_ID=@Act_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@Act_ID", SqlDbType.Int,4)
};
            parameters[0].Value = Act_ID;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条T_SysAction数据
        /// </summary>
        public bool DeleteList(string Act_IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_SysAction ");
            strSql.Append(" where ID in (" + Act_IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DingTalkAgent.Model.SysAction GetModel(int Act_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Act_ID, Page_ID, Act_Name, Controller, Action");
            strSql.Append("  from T_SysAction ");
            strSql.Append(" where Act_ID=@Act_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@Act_ID", SqlDbType.Int,4)
};
            parameters[0].Value = Act_ID;


            DingTalkAgent.Model.SysAction model = new DingTalkAgent.Model.SysAction();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Act_ID"].ToString() != "")
                {
                    model.Act_ID = int.Parse(ds.Tables[0].Rows[0]["Act_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Page_ID"].ToString() != "")
                {
                    model.Page_ID = int.Parse(ds.Tables[0].Rows[0]["Page_ID"].ToString());
                }
                model.Act_Name = ds.Tables[0].Rows[0]["Act_Name"].ToString();
                model.Controller = ds.Tables[0].Rows[0]["Controller"].ToString();
                model.Action = ds.Tables[0].Rows[0]["Action"].ToString();

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据T_SysAction列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_SysAction ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据T_SysAction列表
        /// </summary>
        public IEnumerable<DingTalkAgent.Model.SysAction> IEnumGetList(string strWhere, int Role_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT a.[Act_ID]
                              ,[Page_ID]
                              ,[Act_Name]
                              ,[Controller]
                              ,[Action]
                              ,ISNULL(b.Act_ID,0) as isMe
                          FROM [T_SysAction] a
                          left join (select * from T_Roles_Action where Role_ID=" + Role_ID + @") b on a.Act_ID=b.Act_ID ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            DingTalkAgent.Model.SysAction model;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                model = new DingTalkAgent.Model.SysAction();
                if (dr["Act_ID"].ToString() != "")
                {
                    model.Act_ID = int.Parse(dr["Act_ID"].ToString());
                }
                if (dr["Page_ID"].ToString() != "")
                {
                    model.Page_ID = int.Parse(dr["Page_ID"].ToString());
                }
                if (dr["isMe"].ToString() != "")
                {
                    model.isMe = int.Parse(dr["isMe"].ToString());
                }
                model.Act_Name = dr["Act_Name"].ToString();
                model.Controller = dr["Controller"].ToString();
                model.Action = dr["Action"].ToString();

                yield return model;
            }
        }
        /// <summary>
        /// 获得数据 T_SysAction分页列表
        /// </summary>
        /// <param name="PageIndex">页码</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="total">总记录数</param>
        /// <param name="strWhere">查询语句</param>
        /// <returns></returns>
        public List<DingTalkAgent.Model.SysAction> GetPageList(int PageIndex, int PageSize, out int total, int Page_ID)
        {
            int Skip = (PageIndex - 1) * PageSize;
            //count
            StringBuilder strSqlCount = new StringBuilder();
            strSqlCount.Append("Select @total=count(Act_ID) From T_SysAction ");
            //data
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Top " + PageSize + " * from (");
            strSql.Append("select  row_number() OVER (ORDER BY Act_ID Desc) AS row, Act_ID, Page_ID, Act_Name, Controller, Action ");
            strSql.Append(" FROM  T_SysAction ");
            //where
            StringBuilder Where = new StringBuilder();
            Where.Append(" where Page_ID=@Page_ID");


            strSqlCount.Append(Where.ToString());
            strSql.Insert(0, strSqlCount.ToString() + ";");
            strSql.Append(Where.ToString());
            strSql.Append(") as a ");
            strSql.Append(" Where a.row>" + Skip);

            //parameters
            SqlParameter[] parameters = {
                    new SqlParameter("@total", SqlDbType.Int,4),
                    new SqlParameter("@Page_ID", SqlDbType.Int,4)
                    };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = Page_ID;

            List<DingTalkAgent.Model.SysAction> list = new List<DingTalkAgent.Model.SysAction>();
            DingTalkAgent.Model.SysAction model = new DingTalkAgent.Model.SysAction();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            total = Convert.ToInt32(parameters[0].Value);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                model = new DingTalkAgent.Model.SysAction();
                if (dr["Act_ID"].ToString() != "")
                {
                    model.Act_ID = int.Parse(dr["Act_ID"].ToString());
                }
                if (dr["Page_ID"].ToString() != "")
                {
                    model.Page_ID = int.Parse(dr["Page_ID"].ToString());
                }
                model.Act_Name = dr["Act_Name"].ToString();
                model.Controller = dr["Controller"].ToString();
                model.Action = dr["Action"].ToString();

                list.Add(model);
            }
            return list;
        }
        #endregion
    }

}
